package com.app.synclistview;

import java.util.Vector;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class BookItemAdapter extends BaseAdapter {

	private LayoutInflater inflater = null;
	private Vector<Book> books = new Vector<Book>();
	private ListView listView;
	SyncImageLoader syncImageLoader;
	
	public BookItemAdapter(Context context,
			Vector<Book> books,ListView listView) {
		this.inflater = LayoutInflater.from(context);
		this.books = books;
		this.listView = listView;
		
		listView.setOnScrollListener(onScrollListener);
	}

	public void addBook(Vector<Book> mbook){
		books.addAll(mbook);
	}
	
	@Override
	public int getCount() {
		return books.size();
	}

	@Override
	public Object getItem(int position) {
		return books.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		if(convertView == null){
			convertView = inflater.inflate(R.layout.book_item_adapter, null);
		}
		Book book = books.get(position);
		convertView.setTag(position);
		ImageView iv = (ImageView) convertView.findViewById(R.id.sItemIcon);
		TextView sItemTitle =  (TextView) convertView.findViewById(R.id.sItemTitle);
		TextView sItemInfo =  (TextView) convertView.findViewById(R.id.sItemInfo);
		sItemTitle.setText(book.getBook_name());
		sItemInfo.setText(book.getOut_book_url());
		iv.setBackgroundResource(R.drawable.rc_item_bg);
		syncImageLoader.loadImage(position, book.getOut_book_pic(), imageLoadListener);
		return convertView;
	}
	
	SyncImageLoader.OnImageLoadListener imageLoadListener = new SyncImageLoader.OnImageLoadListener() {
		
		@Override
		public void onImageLoad(Integer it, Drawable drawable) {
			View view = listView.findViewWithTag(it);
			if(view != null){
				ImageView iv = (ImageView) view.findViewById(R.id.sItemIcon);
				iv.setBackgroundDrawable(drawable);
			}
		}
		
		@Override
		public void onError(Integer it) {
			Book model = (Book) getItem(it);
			View view = listView.findViewWithTag(model);
			if(view != null){
				ImageView iv = (ImageView) view.findViewById(R.id.sItemIcon);
				iv.setBackgroundResource(R.drawable.rc_item_bg);
			}
		}
	};
	
	public void loadImage(){
		int start = listView.getFirstVisiblePosition();
		int end =listView.getLastVisiblePosition();
		if(end >= getCount()){
			end = getCount() -1;
		}
		syncImageLoader.setLoadedLimit(start, end);
		syncImageLoader.unlock();
	}
	
	AbsListView.OnScrollListener onScrollListener = new AbsListView.OnScrollListener() {
		
		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			switch (scrollState) {
			case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
				DebugUtil.debug("SCROLL_STATE_FLING");
				syncImageLoader.lock();
				break;
			case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
				DebugUtil.debug("SCROLL_STATE_IDLE");
				syncImageLoader.unlock();
				break;
			case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
				syncImageLoader.lock();
				break;
			default:
				break;
			}
			
		}
		
		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			
		}
	};

	public void clean() {
		books.clear();
	}
}
